//
//  HomeChannelViewController.m
//  DemoV2
//
//  Created by 董一龙 on 2022/6/25.
//

#import "../../Common.h"
#import "HomeChannelViewController.h"
#import "TableViewController.h"
#import "../View/HomeChannelView.h"
#import "../View/HomeTableViewCell.h"


@interface HomeChannelViewController () <UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong, readwrite) HomeChannelView * homeChannelView;
@end

@implementation HomeChannelViewController

- (instancetype)init{
    if(self = [super init]){
        self.tabBarItem.title = @"首页";
        // 不知道为什么图片显示会是灰色, 怎样调整图片大小与文字大小。
        self.tabBarItem.image = kGetImage(@"kele.png");
        self.view.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.homeChannelView = [[HomeChannelView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:self.homeChannelView];
    
    self.homeChannelView.tableView.delegate = self;
    self.homeChannelView.tableView.dataSource = self;
    
    [self.homeChannelView.topView.gameButton addTarget:self action:@selector(clickGameButton) forControlEvents:UIControlEventTouchUpInside];
    [self.homeChannelView.topView.sportButton addTarget:self action:@selector(clickSportButton) forControlEvents:UIControlEventTouchUpInside];
    [self.homeChannelView.topView.moreButton addTarget:self action:@selector(clickMoreButton) forControlEvents:UIControlEventTouchUpInside];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
   return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   UIViewController *controller = [[TableViewController alloc] init];
   HomeTableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
   controller.title = cell.titleLabel.text;
/**
   这是因为从iOS7开始， UIViewController的根view的背景颜色默认为透明色(即clearColor)，所谓”卡顿”其实就是由于透明色重叠后，造成视觉上的错觉，所以这并不是真正的”卡顿”，但这种”卡顿”现象还是让人觉得极其不舒服的，还是务必得解决的！
*/
   controller.view.backgroundColor = [UIColor whiteColor];
   [self.navigationController pushViewController:controller animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
   return 20;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
   return (section + 2) * 2;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
   
   return @"section title";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   HomeTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"id"];
   if(cell == nil) {
       cell = [[HomeTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"id"];
   }
   NSString *titleText = [NSString stringWithFormat:@"title %@ %@", @(indexPath.section), @(indexPath.row)];
   cell.titleLabel.text = titleText;
   cell.leftImageView.image = [UIImage imageNamed:@"baomihua.png"];
   
//    NSString * title = [NSString stringWithFormat:@"主标题 - %@ - %@", @(indexPath.section), @(indexPath.row)];
//    cell.textLabel.text = title;
//   [cell layoutTableViewCell];
   
   return cell;
}

- (void)clickGameButton{
   NSLog(@"Game");
    TableViewController *controller = [TableViewController new];
   controller.title = @"游戏";
   controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickBackButton)];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
   [self presentViewController: nav animated:YES completion:nil];
}
- (void)clickSportButton{
   NSLog(@"Sport");
    TableViewController *controller = [TableViewController new];
   controller.title = @"运动";
   controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickBackButton)];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
   [self presentViewController: nav animated:YES completion:nil];
}
- (void)clickMoreButton{
   NSLog(@"More");
    TableViewController *controller = [TableViewController new];
   controller.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(clickBackButton)];
   UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:controller];
    
//    nav.modalPresentationStyle = UIModalPresentationFullScreen;
//    [nav setNavigationBarHidden:NO];s
   [self presentViewController: nav animated:YES completion:nil];
}

- (void)clickBackButton{
   [self dismissViewControllerAnimated:YES completion:nil];
}


@end
